import 'package:intl/intl.dart';

final DateFormat formatterMs = DateFormat('yyyy-MM-dd HH:mm:ss.SSS');
final DateFormat formatter = DateFormat('yyyy-MM-dd HH:mm:ss');
final DateFormat formatterDate = DateFormat('yyyy-MM-dd');
final DateFormat formatterCompactDate = DateFormat('yyyyMMdd');
final DateFormat formatterCompactTime = DateFormat('HHmmss');
final DateFormat formatterCompactTimeMs = DateFormat('HHmmssSSS');


/// 格式：yyyy-MM-dd HH:mm:ss
String toString(DateTime dt) {
  return formatter.format(dt);
}

/// 格式：yyyy-MM-dd HH:mm:ss.SSS
String toMsString(DateTime dt) {
  return formatterMs.format(dt);
}

final DateFormat _parserCompactMs = DateFormat('yyyy|MM|dd|HH|mm|ss|SSS');
final DateFormat _formatterCompactMs = DateFormat('yyyyMMddHHmmssSSS');
/// 格式yyyyMMddHHmmssSSS
DateTime parseCompactDateTimeMs(String str) {
  str = str.replaceRange(3, 4, '${str[3]}|');
  str = str.replaceRange(6, 7, '${str[6]}|');
  str = str.replaceRange(9, 10, '${str[9]}|');
  str = str.replaceRange(12, 13, '${str[12]}|');
  str = str.replaceRange(15, 16, '${str[15]}|');
  str = str.replaceRange(18, 19, '${str[18]}|');
  return _parserCompactMs.parse(str);
}

String formatCompactDateTimeMs(DateTime dt) {
  return _formatterCompactMs.format(dt);
}

final DateFormat _parserCompact = DateFormat('yyyy|MM|dd|HH|mm|ss');
final DateFormat _formatterCompact = DateFormat('yyyyMMddHHmmss');
/// 格式yyyyMMddHHmmss
DateTime parseCompactDateTime(String str) {
  str = str.replaceRange(3, 4, '${str[3]}|');
  str = str.replaceRange(6, 7, '${str[6]}|');
  str = str.replaceRange(9, 10, '${str[9]}|');
  str = str.replaceRange(12, 13, '${str[12]}|');
  str = str.replaceRange(15, 16, '${str[15]}|');
  return _parserCompact.parse(str);
}

String formatCompactDateTime(DateTime dt) {
  return _formatterCompact.format(dt);
}